﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Data;
using JKF.DataModule.SqlServerDAL;
using JKF.Substructure;
using JKF.Entities.Model;

namespace JKF.BusinessLayer.BLL
{
   public partial class bllSYS_ROLE
    {
       /// <summary>
        /// 取角色列表
       /// </summary>
       /// <param name="type">1,表示用户列表页面加载。2表示用户添加和修改页面加载</param>
       /// <returns></returns>
        public string GetSysRoleArea(int type)
        {
            //const string cacheName = "SysRoleOne";
            //if (!CacheManage.HadCache(cacheName))
            //{
                var sb = new StringBuilder();
                // 判断当前用户的角色去加载对应的角色下拉列表
                SYS_ROLEinfo roleInfo = new  bllSYS_ROLE().GetModel(bllSYS_USER.CurrentUser.ROLE_KEY);
                if (roleInfo == null)
                {
                    sb.AppendFormat("<option value=\"{0}\">{1}</option>","-1","获取角色Select失败，请与管理员联系！");
                    //CacheManage.AddCache(cacheName, sb.ToString());
                    //return CacheManage.GetCache(cacheName).ToString();
                    return sb.ToString();
                }
                string now_role_name = roleInfo.ROLE_NAME;
                List<SYS_ROLEinfo> oList = new List<SYS_ROLEinfo>();
                if (SysConfig.HeadShop.Contains(now_role_name))
                {
                    oList = this.GetModelList(" "); // 加载全部
                }
                else if (SysConfig.AreaShop.Contains(now_role_name))
                {
                    StringBuilder condition = new StringBuilder();
                    condition.AppendFormat("role_name not like '%{0}%' ", SysConfig.HeadShop);
                    string[] roleStrs = SysConfig.AreaShop.Split(new char[] { ','}, StringSplitOptions.RemoveEmptyEntries);
                    for (int i = 0; i < roleStrs.Length; i++)
                    {
                        condition.AppendFormat("and  role_name not like '%{0}%'",roleStrs[i]);
                    }
                    oList = this.GetModelList(condition.ToString());
                }
                else if (SysConfig.LocalShop.Contains(now_role_name))
                {
                    StringBuilder condition = new StringBuilder();
                    string[] roleStrs = SysConfig.OperatorShop.Split(new char[] { ',' }, StringSplitOptions.RemoveEmptyEntries);
                    for (int i = 0; i < roleStrs.Length; i++)
                    {
                        condition.AppendFormat("or role_name  like '%{0}%'", roleStrs[i]);
                    }
                    oList = this.GetModelList(condition.ToString().Remove(0,2));
                }
                if (oList.Count == 1 && type==2)
                {
                    //表明是操作员
                    sb.AppendFormat("<option value=\"{0}\" selected=\"selected\">{1}</option>", oList[0].ROLE_KEY, oList[0].ROLE_NAME);
                }
                else
                {
                    foreach (var item in oList)
                    {
                        sb.AppendFormat("<option value=\"{0}\">{1}</option>", item.ROLE_KEY, item.ROLE_NAME);
                    }
                }
                //CacheManage.AddCache(cacheName, sb.ToString());
            //}
                return sb.ToString();
            //return CacheManage.GetCache(cacheName).ToString();
        }

       /// <summary>
        /// 根据角色role_key获取角色名称
        /// </summary>
        /// <param name="role_key"></param>
        /// <returns></returns>
       public  string GetRoleNameByKeyBLL(string role_key)
       {
           return dalSYS_ROLE.GetRoleNameByKeyDAL(role_key);
       }
       /// <summary>
        /// 获取角色列表
        /// </summary>
        /// <param name="ROLE_NAME">角色名称</param>
        /// <param name="pageCurrent">当前页码</param>
        /// <param name="pageSize">一页显示多少</param>
        /// <param name="pageCount">总页数</param>
        /// <param name="rowCount">总记录数</param>
        /// <returns>返回DataTable数据</returns>
       public  DataTable GetSys_RolesBLL(string ROLE_NAME, int pageCurrent, int pageSize, ref int pageCount, ref int rowCount)
       {
           return dalSYS_ROLE.GetSys_RolesDAL(ROLE_NAME, pageCurrent, pageSize, ref pageCount, ref rowCount);
       }

        /// <summary>
        /// 判断是否存在此名称,true表示存在
        /// </summary>
        /// <param name="shop_name">角色名称</param>
        /// <returns></returns>
       public  bool ExistsSysRoleNameBLL(string sys_role_name)
       { 
           return dalSYS_ROLE.ExistsSysRoleNameDAL(sys_role_name);
       }
       /// <summary>
       /// 采用事务提交生成的Insert语句
       /// </summary>
       /// <param name="sql"></param>
       public void TransactionInsertRole_Module_ActionBLL(string sql)
       {
           dalSYS_ROLE.TransactionInsertRole_Module_ActionDAL(sql);
       }
       /// <summary>
        /// 采用事务提交Role生成的Update语句
        /// </summary>
        /// <param name="sql"></param>
       public  void TransactionUpdateRole_Module_ActionBLL(string sql)
       {
           dalSYS_ROLE.TransactionUpdateRole_Module_ActionDAL(sql);
       }
       /// <summary>
        /// 采用事务提交Role生成的delete语句
       /// 此方法作废。改用存储过程
       /// 修改日期：2013.1.15
        /// </summary>
        /// <param name="sql"></param>
       public  void TransactionDeleteRole_Module_ActionBLL(string sql)
       {
           dalSYS_ROLE.TransactionDeleteRole_Module_ActionDAL(sql);
       }
        /// <summary>
        /// 删除角色，调用事务存储过程
        /// 日期：2013.1.15
        /// </summary>
        /// <param name="role_key"></param>
        /// <returns></returns>
       public  string StoreProcedureDeleteRole_Module_ActionDAL(string role_key)
       {
           return dalSYS_ROLE.StoreProcedureDeleteRole_Module_ActionDAL(role_key);
       }
    }
}
